/**
 * Created by zhanglihe on 15/11/10.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog'
], function ($, common, ready, Collection, template, Model, dialog) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            var _this = this;
            //分类
            $.ajax({
                url: _global.url.api + 'categorys',
                type: 'get',
                async: false,
                data: {depth: 1},
                success: function (res) {
                    if (res.code == 0) {
                        _this.set('category', res.data);
                    }
                }
            });
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'jd_goodses',
                condition: {limit: 10, offset: 0},
                model: {
                    url: _global.url.api + 'jd_goods/{id}',
                    event: {
                        '.item-edit:click': 'edit',
                        '.item-on-sale:click': 'onSale',
                        '.item-off-sale:click': 'offSale',
                        '.item-copy-url': 'copyUrl'
                    },
                    onSale: function (e) {
                        e.preventDefault();
                        this.data.status = 1;
                        this.formData = {status: 1};
                        this.put();
                    },
                    offSale: function (e) {
                        e.preventDefault();
                        this.data.status = 2;
                        this.formData = {status: 2};
                        this.put();
                    },
                    copyUrl: function (e) {
                        e.preventDefault();
                        dialog({
                            content: this.get_data('wap_url') + '<br>请全选并点右键复制',
                            ok: function () {
                            }
                        }).show(e.target);
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
            this.showCategory();
        },
        showCategory: function () {
            var _this = this;
            //console.log(_this.category);return false;
            //一级分类
            $.each(_this.category, function (n, v) {
                if (v.level == 1) {
                    $('[name="category"]').append('<option value="' + v.id + '">' + v.name + '</option>');
                }

            });
            //显示二级分类，并联动
            //bind change event
            $('[name="category"]').on('change', function () {
                var id = $(this).val();

                $('[name="child-cate"]').hide();

                $('[name="child-cate"]').html('<option value="0">所有小类</option>').show();
                $.ajax({
                    url: _global.url.api + 'categorys',
                    type: 'get',
                    async: false,
                    data: {depth: 1,pid:id},
                    success: function (res) {
                        if (res.code == 0) {
                            $.each(res.data, function (n, v) {
                                $('[name="child-cate"]').append('<option value="' + v.id + '">' + v.name + '</option>');
                            });
                        }
                    }
                });

            });
        },
        set: function ($k, $v) {
            this[$k] = $v;
        },
        event: function () {
            var _this = this;
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = $('input[name="keyword"]').val();
                var category = $('select[name="category"]').val();
                var child_cate = $('select[name="child-cate"]').val();
                _this.collection.condition.category = (child_cate != undefined && child_cate != 0) ? child_cate : category;
                _this.collection.reload();
            });

            /**
             * 按状态查询
             */
            $('#btn-status').find('a').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.keyword = '';
                _this.collection.condition.status = $(e.target).attr('rel');
                _this.collection.reload();
                $(e.target).addClass('active').siblings().removeClass('active');
            });
        }
    };

    new index();

});